package com.sti.framework.models.User.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sti.framework.core.annotation.authorization.Authorization;
import com.sti.framework.core.data.IDEntity;
import com.sti.framework.core.web.Page;
import com.sti.framework.core.web.ResponseBodyEntity;
import com.sti.framework.core.web.SimplePage;
import com.sti.framework.models.System.data.MenuRoleMngae;
import com.sti.framework.models.System.service.MenuRoleMngaeService;
import com.sti.framework.models.User.data.Role;
import com.sti.framework.models.User.service.RoleAuthorityService;
import com.sti.framework.models.User.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.validation.Valid;

@RestController
@RequestMapping("/Api/Role")
@Api(tags = "角色管理", value = "用户管理")
public class RoleController {

    @Inject
    private RoleService roleService;

    @Inject
    private RoleAuthorityService roleAuthorityService;
    @Inject
    private MenuRoleMngaeService menuRoleMngaeService;


    @ApiOperation(value = "角色列表")
    @ApiImplicitParam(name = "token", value = "登陆后获取的签名", paramType = "header", dataTypeClass = String.class, required = true)
    @Authorization(tag = "/List", describes = "角色列表")
    @GetMapping("/List")
    public ResponseBodyEntity<SimplePage<Role>> List(Page page) {
        return ResponseBodyEntity.success(roleService.getAll(page));
    }

    @ApiOperation(value = "角色列表")
    @ApiImplicitParam(name = "token", value = "登陆后获取的签名", paramType = "header", dataTypeClass = String.class, required = true)
    @Authorization(tag = "/List", describes = "角色列表")
    @GetMapping("/ListAll")
    public ResponseBodyEntity<SimplePage<Role>> ListAll() {
        return ResponseBodyEntity.success(roleService.list());
    }

    @ApiOperation(value = "角色详情")
    @ApiImplicitParam(name = "token", value = "登陆后获取的签名", paramType = "header", dataTypeClass = String.class, required = true)
    @Authorization(tag = "/List", describes = "角色列表")
    @GetMapping("/Info")
    public ResponseBodyEntity<Role> Info(@Valid IDEntity id) {
        return ResponseBodyEntity.success(roleService.get(id.getId()));
    }

    @ApiOperation(value = "增加角色")
    @ApiImplicitParam(name = "token", value = "登陆后获取的签名", paramType = "header", dataTypeClass = String.class, required = true)
    @Authorization(tag = "/Create", describes = "增加角色")
    @PostMapping(value = "/Create")
    public ResponseBodyEntity Create(@RequestBody @Valid Role role) {
        roleService.saveData(role);
        return ResponseBodyEntity.success();
    }

    @ApiOperation(value = "修改角色")
    @ApiImplicitParam(name = "token", value = "登陆后获取的签名", paramType = "header", dataTypeClass = String.class, required = true)
    @Authorization(tag = "/Modify", describes = "修改角色")
    @PostMapping(value = "/Modify")
    public ResponseBodyEntity Modify(@RequestBody @Valid Role role, IDEntity id) {
        roleService.updateData(role);
        return ResponseBodyEntity.success();
    }

    @ApiOperation(value = "删除角色")
    @ApiImplicitParam(name = "token", value = "登陆后获取的签名", paramType = "header", dataTypeClass = String.class, required = true)
    @Authorization(tag = "/Remove", describes = "删除角色")
    @PostMapping(value = "/Remove")
    public ResponseBodyEntity Remove(@RequestBody @Valid IDEntity id) {
        roleService.delete(id.getId());

        // 删除关联的权限设置
        roleAuthorityService.deleteForRole(Long.valueOf(id.getId()));
        return ResponseBodyEntity.success();
    }

    /**
     * 根据角色ID获取权限数据
     * @param roleId
     * @return
     */
    @Authorization(tag = "/List", describes = "角色列表")
    @GetMapping("/RoleMenuInfo")
    public ResponseBodyEntity roleMenuInfo(Long roleId) {
        LambdaQueryWrapper<MenuRoleMngae> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MenuRoleMngae::getRoleId,roleId);
        return ResponseBodyEntity.success(menuRoleMngaeService.list(lambdaQueryWrapper));
    }
}
